package com.javandad.app.server.servlet;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpSession;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.javandad.app.client.MessageBoxService;
import com.javandad.app.server.db.DatabaseManager;
import com.javandad.app.server.db.PMFSingleton;
import com.javandad.app.server.mail.MailHandler;
import com.javandad.app.server.mail.entity.MessageMail;
import com.javandad.app.shared.entity.Friendship;
import com.javandad.app.shared.entity.Message;
import com.javandad.app.shared.entity.Notification;
import com.javandad.app.shared.entity.project.ProjectAssoc;

/**
 * The server side implementation of the RPC service.
 */
public class MessageBoxServiceImpl extends RemoteServiceServlet implements
		MessageBoxService {
	private static final long serialVersionUID = 559457991164277329L;

	@Override
	public List<Message> downloadMessages(String username, boolean sent)
			throws IllegalArgumentException {
		PersistenceManager pm = PMFSingleton.getPM();
		List<Message> msgs = null;
		if (sent) {
			msgs = DatabaseManager.getDefaultDatabaseManager().getMessages(
					username, null, pm);
		} else {
			msgs = DatabaseManager.getDefaultDatabaseManager().getMessages(
					null, username, pm);
		}
		pm.close();
		return msgs;
	}

	public Boolean uploadMessage(final Message msg)
			throws IllegalArgumentException {

		boolean result = false;

		msg.setDateOfDelivery(new Date());
		PersistenceManager pm = PMFSingleton.getPM();
		pm.makePersistent(msg);

		MailHandler.sendMail(new MessageMail(msg));
		result = true;
		return result;
	}

	public List<String> getSessionInfo() throws IllegalArgumentException {
		List<String> list = new LinkedList<String>();
		HttpSession session = this.getThreadLocalRequest().getSession();
		list.add((String) session.getAttribute("mode"));
		list.add((String) session.getAttribute("username"));
		return list;
	}

	public List<Notification> downloadNotifications(String username)
			throws IllegalArgumentException {
		LinkedList<Notification> res = new LinkedList<Notification>();
		PersistenceManager pm = PMFSingleton.getPM();
		res.addAll(DatabaseManager.getDefaultDatabaseManager().getInvitations(
				username, pm));
		res.addAll(DatabaseManager.getDefaultDatabaseManager()
				.getParticipationInvitations(username, pm));
		pm.close();
		return res;
	}

	@Override
	public Boolean replyInvitation(Friendship invitation, Boolean reply)
			throws IllegalArgumentException {
		PersistenceManager pm = PMFSingleton.getPM();
		if (reply) {
			invitation.setDateOfConfirmation(new Date());
			pm.makePersistent(invitation);
		} else {
			Friendship f = pm.getObjectById(Friendship.class, invitation.getId());
			pm.deletePersistent(f);
			
		}
		pm.close();
		return true;
	}

	@Override
	public boolean respondParticipationInvitation(ProjectAssoc p, boolean response) {
		PersistenceManager pm = PMFSingleton.getPM();
		if (response) {
			p.setDateBegin(new Date(System.currentTimeMillis()));
			pm.makePersistent(p);
		} else {
			pm.deletePersistent(p);
		}
		pm.close();
		return true;
	}
}
